/**
 * [SNTouch depend on jQuery 1.7.2]
 * 
 * @author 12050231
 *
 */

var SNTouch = SNTouch || {
	init : function(){
		return this.ready();
	},
	ready : function(){
		this.hideBar();
		this.Widget.slide({
			hook : "#J_index_slide",
			effect : "scroll"
		})
	},
	hideBar : function(){
		setTimeout(scrollTo, 0, 0, 1);
	},

};

SNTouch.Widget = {
	slide : function(settings){
		var defaults = {
			hook : "#Slide",
			direction : "horizon",
			slideBox : ".index-slide-box",
			slideUl : ".slide_ul",
			slideLi : ".slide_ul li",
			prev : ".prev",
			next : ".next",
			disable : "disable",
			counter : ".trigger",
			countStyle : "dot",
			effect : "both",
			current : 1,
			delay : 300,
			autoplay : 1,
			cycle : 1
		};
		if(settings){
			$.extend(defaults, settings);
		}
		return function(o){
			var w = 0, h = 0;
			var cur = o.current;
			var hook = $(o.hook);
			var len = Math.ceil(hook.find(o.slideLi).length);
			var li = hook.find(o.slideLi);
			var ul = hook.find(o.slideUl);
			var width = hook.find(defaults.slideBox).width();
			var height = hook.find(o.slideBox).height();
			//判断滚动类型
			if(o.direction == "horizon"){
				for(var i = 0; i < len; i++){
					w += li.eq(i).width();
				}
				ul.width(w);
				//索引位置
				hook.find(o.slideUl).css({
					"left" : -(width * (cur - 1))
				})
			}else{
				for(var i = 0; i < len; i++){
					h += li.eq(i).width();
				}
				ul.height(h);
				hook.find(o.slideUl).css({
					"top" : -(height * (cur - 1))
				})
			}
			Ctrl();
			switch (o.effect){
				case "slide":
					A();
				case "scroll":
					B();
				case "both":
					A();
					B();
			}

			//Function
			/**
			 *  可以控制切换
			 */	
			function Ctrl() {
	            if (o.cycle != 1) {
	                var prev = hook.find(o.prev),
	                    next = p.find(o.next);
	                prev.removeClass(o.disable);
	                next.removeClass(o.disable);
	                if (o.current == 1) {
	                    prev.addClass(o.disable)
	                } else {
	                    if (o.current == len) {
	                        A.addClass(o.disable)
	                    }
	                }
	            }
	        }
	        //normal function
	        function A(){

        	}
        	//scroll function
	        function B(){
	        	//距离基数
	        	if(o.direction == "horizon"){
	        		var dis = -w + width;
	        	}else{
	        		var dis = -h + height;
	        	}
	        	var x = 0;
	        	//绑定click方法
	        	ul[0].onmousedown = ul[0].ontouchstart = DragStart;

	        	function DragStart(para){
	        		var aPos, bPos, c;
	        		if(o.autoplay == 1){
	        			auto.process();
	        		}
	        		//获取匹配元素相对父元素的偏移坐标
	        		//初始状态
	        		var pos = [ul.position().left, ul.position().top];
	        		console.log(pos[0])
	        		//touchmove 初始坐标
	        		aPos = getPos(para);
	        		ul[0].ontouchmove = ul[0].onmousemove = DragMove;
	        		//开始拖动
	        		function DragMove(e){
	        			//移动的距离坐标
	        			bPos = getPos(e);
	        			if(o.direction == "horizon"){
	        				//移动的距离差
	        				var dPos = (bPos[0] - aPos[0]) + pos[0];
	        				//if(Math.abs(bPos[0] - aPos[0]) - Math.abs(bPos[1] - aPos[1]) > 0){
		        				e.preventDefault();
		        				setPos();
		        				ul[0].ontouchend = document.onmouseup = DragEnd;
		        			//}
	        			}else{
	        				var dPos = (bPos[1] - aPos[1]) + pos[1];
		        			e.preventDefault();
		        			setPos();
		        			ul[0].ontouchend = document.onmouseup = DragEnd;
	        			}
	        			//拖动位置
	        			function setPos(){
	        				if(dPos <= 0 && dPos >= dis){
	        					if(o.direction == "horizon"){
	        						ul[0].style.left = dPos + "px";
	        					}else{
	        						ul[0].style.top = dPos + "px";
	        					}
	        				}
	        			}

	        		}

	        		function DragEnd(e){
	        			var dPos, F, ePos = getPos(e);
	        			if(o.autoplay == 1){
	        				auto.process();
	        			}
	        			if(o.direction == "horizon"){
	        				dPos = ePos[0] - aPos[0];
	        				stopDrag(e)
	        			}else{
	        				dPos = ePos[1] - aPos[1];
	        				stopDrag(e)
	        			}
	        			function stopDrag(e){
	        				if(dPos < -5){
	        					//
	        					clear.process(left);
	        				}else{
	        					if(dPos > 5){
	        						//
	        						clear.process(right)
	        					}
	        				}
	        			}
	        		}
	        		Ctrl();
	        		//ul[0].ontouchmove = ul[0].ontouchend = ul[0].onmousemove = document.onmouseup = null


	        	}
	        }

	        //即时获取鼠标坐标
    		function getPos(e){
    			var posArr = [];
    			posArr[0] = e.changeTouches ? e.changeTouches[0].clientX : e.clientX;
    			posArr[1] = e.changeTouches ? e.changeTouches[0].clientY : e.clientY;
    			return posArr;
    		}
    		var left = function(){
    			if(o.autoplay == 1){
    				auto.process();
    			}
    			if(o.cycle == 1){
    				if(cur != len){
    					l();
    					return false;
    				}else{
    					l();
    					li.eq(0).css("left", width*cur);
    					hook.find(o.slideBox).css("left", 0);
    					return false;
    				}
    			}else{
    				if(cur != len){
    					l();
    					return false;
    				}
    			}
    		}
    		function l(){
    			if(o.direction == "horizon"){
    				ul.animate({
    					left : -(width * cur)
    				}, o.timer, function(){
    					Ctrl();
    					ul.css({
    						"left" : -(width * (cur - 1))
    					});
    					li.eq(0).css("left", 0);
    				});
    			}else{
    				ul.animate({
    					top : -(height * cur)
    				}, o.timer)
    			}
    			cur == len ? cur = 1 : cur++;
    		}
    		var right = function(){
    			if(o.autoplay == 1){
    				auto.process();
    			}
    			if(o.cycle == 1){
    				if(cur != 1){
    					r();
    					return false;
    				}else{
    					r();
    					li.eq(cur -1).css({
    						"left" : -(width * cur)
    					});
    					li.eq(len - 1).css("left", -(cur * width));
    					li.eq(0).css("left", 0);
    					return false;
    				}

    			}else{
    				if(cur != 1){
    					r();
    					return false;
    				}
    			}
    		}
    		function r(){
    			if(o.direction == "horizon"){
    				ul.animate({
    					left : -(width * (cur - 2))
    				}, o.timer, function(){
    					Ctrl();
    					ul.css("left", -(width * (cur - 1)));
    					li.eq(len -1).css("left", 0);
    				})
    			}else{
    				ul.animate({
    					top : -(height * (k - 2))
    				}, c.timer,function(){})
    			}
    			cur == 1 ? cur = len : cur --;
    		}
	        var auto = {
                timeoutId: null,
                performProcessing: function() {
                	try{
                		l();
                	}catch(e){
                		console.log(e);
                	}
                    
                },
                process: function() {
                    clearInterval(this.timeoutId);
                    this.timeoutId = setInterval(function() {
                        auto.performProcessing()
                    }, 2000);
                },
                dispose: function() {
                    clearInterval(this.timeoutId);
                    return;
                }
            };

            var clear = {
            	timerid: null,
                action: function(x) {
                	try{
                		x();
                	}catch(e){

                	}
                    
                },
                process: function(x) {
                    clearTimeout(this.timerid);
                    this.timerid = setTimeout(function() {
                        clear.action(x)
                    }, o.timer)
                }
            };
            if (o.autoplay == 1) {
                auto.process();
            }
		}(defaults);
		
		
			
        




	}
}






















SNTouch.init();




















// SNTouch.Widget = {};
// (function(S){
// 	S.extend(S, {
// 		Switchable : function(){
// 			alert(54)
// 		}
// 	});
// })(SNTouch)






























console.log(SNTouch)